package com.qianfeng.openapi.monitor.utils;

import com.qianfeng.openapi.monitor.job.MyJOb;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

/**
 * @Description:
 * @Company: 千锋互联
 * @Author: 李丽婷
 * @Date: 2021/1/5
 * @Time: 下午2:12
 */
public class QuartzUtils {


    public  static  void  createTask(String  jobName,String cron,String triggerName){
        try {
            //1.jobdetail
            JobDetail jobDetail = JobBuilder.newJob(MyJOb.class)
                    .withIdentity(jobName,"myGroup").build();
            //2.触发任务
            Trigger trigger = TriggerBuilder.
                    newTrigger().
                    withSchedule(CronScheduleBuilder.cronSchedule(cron))
                    .withIdentity(triggerName,"myGroup").build();
            //3.创建Scheduler (spring注入  或者自己创建)
            SchedulerFactory schedulerFactory = new StdSchedulerFactory();
            Scheduler scheduler = schedulerFactory.getScheduler();

            //4.注册
            scheduler.scheduleJob(jobDetail,trigger);

            scheduler.start();

        } catch (SchedulerException e) {
            e.printStackTrace();
        }


    }

}
